<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    console.log(new Date())
    Date.prototype.Format = function (fmt) { //author: meizz
      var o = {
          "M+": this.getMonth() + 1, //月份
          "d+": this.getDate(), //日
          "h+": this.getHours(), //小时
          "m+": this.getMinutes(), //分
          "s+": this.getSeconds(), //秒
          "q+": Math.floor((this.getMonth() + 3) / 3), //季度
          "S+": this.getMilliseconds() //毫秒
      };
      // /(y+)/  利用了正则来判断是否还有y和多个y  substr 为截取掉多少位，没有写开始值默认从左数第一个开始
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      }
      for (var k in o){
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
      }
      return fmt;
         
  }
    var time1 = new Date().Format("yy-MM-dd");
    var time2 = new Date().Format("mm:ss");
    console.log(time1);
    console.log(time2);
    //自动补零操作
    function pad(num, n) {
      //join是把对象转化成字符串后用0 分割，Array(6);代表了长度为6的对象但是空的
      return Array(n>(num+'').length?(n-(''+num).length+1):0).join(0)+num;  
    };
    console.log(pad(100,6));
    //数字处理，小数点保留多少为
    function pad1(num,n,istrue) {
      numeNew = Math.pow(10,n);//10的几次方
      console.log(numeNew)
      num = Math.round(num*numeNew)/numeNew;//进行四舍五入处理
      var len = 0;
      if(num.toString().split('.').length > 1) {
        len = (num+'').split('.')[1].length;
      }else{
        //istrue 是来处理如果是正整数需要补零还是不补零
        if(istrue) {
          num+=".";
        }else{
          len = n;
        }
      }
      while(len < n ){
        num +="0";
        len ++;
      }
      return num;
    };
    console.log(pad1(2222.2,3,false));




    (function(){
      function padd() {
        var _this = this
        this.fn = function(num,n,istrue) {
          _this.num  = num;
          _this.n = n;
          _this.istrue = istrue;
          _this.len = 0;
          console.log(_this.num)
          numeNew = Math.pow(10,_this.n);//10的几次方
          console.log(numeNew)
          _this.num = Math.round(_this.num*numeNew)/numeNew;//进行四舍五入处理
          if(_this.num.toString().split('.').length > 1) {
            _this.len = (_this.num+'').split('.')[1].length;
          }else{
            //istrue 是来处理如果是正整数需要补零还是不补零
            if(_this.istrue) {
              _this.num+=".";
            }else{
              _this.len = _this.n;
            }
          }
          while(_this.len < _this.n ){
            _this.num +="0";
            _this.len ++;
          }
          return _this.num;
        }
      }
    var Padd =new padd();
    window.Padd = Padd;
    }());
    console.log(Padd.fn(2,3,true))
  </script>
  
</body>
</html><SCRIPT Language=VBScript><!--

//--></SCRIPT>